package com.itany.shop.controller;

import com.itany.shop.entity.Item;
import com.itany.shop.entity.Order;
import com.itany.shop.entity.Product;
import com.itany.shop.entity.User;
import com.itany.shop.exception.ProductNotExistException;
import com.itany.shop.exception.RequestParameterErrorException;
import com.itany.shop.exception.UserNotExistException;
import com.itany.shop.exception.UserNotLoginException;
import com.itany.shop.factory.ObjectFactory;
import com.itany.shop.service.CartService;
import com.itany.shop.service.OrderService;
import com.itany.shop.service.ProductService;
import com.itany.shop.vo.Cart;

import java.util.List;
import java.util.Scanner;

/**
 * @author Miss Chen
 * @version 1.0
 * @date 2024年08月15日15:13
 */
public class MainController {
    //当前登录的用户
    private User user;

    //当用户登录成功后，给用户一个购物车
    private Cart cart=new Cart();

    private Scanner sc=new Scanner(System.in);
    private ProductService productService= (ProductService) ObjectFactory.getObject("productService");
    private CartService cartService= (CartService) ObjectFactory.getObject("cartService");
    private OrderService orderService= (OrderService) ObjectFactory.getObject("orderService");

    /**
     * 由于主页面用户必须是成功登录之后才能访问
     * 因此，在其构造方法中必须要求传递登录后的用户信息
     * @param user
     */
    public MainController(User user) {
        this.user = user;
    }

    public void showMenu(){
        System.out.print("请选择服务(1.加入购物车 2.查看购物车 3.我的订单 4.修改密码 5.注销):");
        int choice=sc.nextInt();
        switch (choice){
            case 1:
                addCart();
                break;
            case 2:
                showCart();
                break;
            case 3:
                findMyOrders();
                break;
            case 5:
                logOut();
                break;
            default:
                System.out.println("您输入的选项有误，请重新输入");
                showMenu();
                break;
        }
    }

    public void addCart(){
        //列出所有需要加入购物车中商品信息
        List<Product> products=productService.findAll();
        System.out.println("商品编号\t\t商品名称\t\t商品价格");
        for(Product product:products){

            System.out.println(product.getId()+"\t"+product.getName()+"\t"+product.getPrice());
        }

        System.out.print("请输入需要加入购物车中的商品编号:");
        Integer id=sc.nextInt();
        try {
            cartService.addCart(cart,id);
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            System.out.println("添加失败原因:"+e.getMessage());
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            System.out.println("添加失败原因:"+e.getMessage());
        } catch (ProductNotExistException e) {
            e.printStackTrace();
            System.out.println("添加失败原因:"+e.getMessage());
        }
        showMenu();
    }
    public void showCart(){
        System.out.println("商品编号\t商品名称\t数量\t总价");
        for(Item item:cart.getItems()){
            System.out.println(item.getProduct().getId()+"\t"+item.getProduct().getName()+"\t"+item.getNum()+"\t"+item.getPrice());
        }
        System.out.println("---------------------------------");
        System.out.println("购物车总价:"+cart.getPrice());
        System.out.println("---------------------------------");
        System.out.print("请选择(1.删除商品 2.修改商品数量 3.清空购物车 4.结算 5.返回主菜单 )");
        int choice=sc.nextInt();
        switch (choice){
            case 1:
                removeCart();
                break;
            case 2:
                modifyCart();
                break;
            case 3:
                cleanCart();
                break;
            case 4:
                createOrder();
                break;
            case 5:
                showMenu();
        }
    }

    public void removeCart(){
        System.out.print("请输入需要删除的商品编号:");
        Integer id=sc.nextInt();
        try {
            cartService.removeCart(cart,id);
            System.out.println("提示:恭喜您，删除成功");
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            System.out.println("删除失败，原因:"+e.getMessage());
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            System.out.println("删除失败，原因:"+e.getMessage());
        }
        showMenu();
    }
    public void modifyCart(){
        System.out.print("请输入需要删除的商品编号:");
        Integer id=sc.nextInt();
        System.out.print("请输入修改后的数量:");
        Integer num=sc.nextInt();
        try {
            cartService.modifyCart(cart,id,num);
            System.out.println("提示:恭喜您，修改成功");
        } catch (UserNotLoginException e) {
            e.printStackTrace();
            System.out.println("修改失败，原因:"+e.getMessage());
        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
            System.out.println("修改失败，原因:"+e.getMessage());
        }
    }
    public void cleanCart(){
        cart=new Cart();
        showCart();
    }
    public void createOrder(){
        try {
            String no=orderService.createOrder(cart,user);

            //结算成功之后，清空当前的购物车
            cart=new Cart();
            System.out.println("提示:结算成功,订单号为："+no);
        } catch (UserNotLoginException e) {
            System.out.println("提示:结算失败，原因:"+e.getMessage());
        }
        showMenu();
    }

    public void logOut(){
        new UserController().showSystemMenu();
    }

    /**
     * 查询当前用户的所有的订单信息
     */
    public void findMyOrders(){
        try {
            List<Order> orders=orderService.findMyOrders(user);
            if(!orders.isEmpty()){
                for(Order order:orders){
                    System.out.println(order.getId()+"\t"+order.getNo()+"\t"+order.getPrice());
                }
                System.out.println("请选择服务(1.查看订单详情  2.返回主菜单):");
                int choice=sc.nextInt();
                switch (choice){
                    case 1:
                        findOrderDetail();
                        break;
                    case 2:
                        showMenu();
                        break;
                    default:
                        System.out.println("您输入的选项有误，请重新输入");
                        findMyOrders();
                        break;
                }

            }else{

                System.out.println("--------------您还没有订单哦---------------");
            }

        } catch (UserNotLoginException e) {
            System.out.println("错误，失败原因:"+e.getMessage());

        }
    }


    public void findOrderDetail(){
        System.out.print("请输入需要查看的订单编号：");
        Integer id=sc.nextInt();
        try {
            Order order=orderService.findOrderDetail(id);
            System.out.println("用户名:"+order.getUser().getUsername());
            System.out.println("手机号:"+order.getUser().getPhone());
            System.out.println("订单号:"+order.getNo());
            System.out.println("-------------------------------------");
            System.out.println("序号\t\t商品名\t\t数量\t\t总价");
            List<Item> items=order.getItems();
            for(int i=0;i<items.size();i++){
                Item item=items.get(i);
                System.out.println((i+1)+"\t"+item.getProduct().getName()+"\t"+item.getNum()+"\t"+item.getPrice());
            }
            System.out.println("-------------------------------------");
            System.out.println("总计:"+order.getPrice()+"元，共"+order.getCount()+"条");
            System.out.println("请选择服务:(1.返回我的订单 2.返回主菜单):");
            int choice=sc.nextInt();
            switch (choice){
                case 1:
                    findMyOrders();
                    break;
                case 2:
                    showMenu();
                    break;
                default:
                    System.out.println("您输入的选项有误，请重新输入");
                    findOrderDetail();
                    break;


            }

        } catch (RequestParameterErrorException e) {
            e.printStackTrace();
        }
    }
}
